using System;
using System.Data.OleDb;

namespace DataAccessTecnoPez
{
     class cnn
    {
        private static SQL objSql;
        public static SQL getInstance()
        {
            if (objSql == null)
            {
                objSql = new SQL(getConectionString);
            }
            return objSql;
        }
        public static string getConectionString
        {
            get
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings["SQLLocal"].ConnectionString;
            }
        }
        public static void cerrar()
        {
            if (objSql != null)
            {
                objSql.Close();
                objSql = null;
            }
        }


        public static bool estaConectado
        {
            get
            {
                if (objSql != null)
                {
                    return objSql.IsConnected;
                }
                else
                {
                    return false;
                }
            }
        }

        public static void cerrarConexion()
        {
            getInstance().Close();
        }


        public static OleDbDataReader getDataReader(string sql)
        {
            cnn.getInstance().SqlString = sql;
            return cnn.getInstance().GetDataReader();
        }

        public static OleDbDataReader getDataReader(string sql, params object[] parametros)
        {
            cnn.getInstance().SqlString = sql;
            return cnn.getInstance().GetDataReader(parametros);
        }

        public static int ejecutarSQL(string sql, params object[] parametros)
        {
            cnn.getInstance().SqlString = sql;
            return cnn.getInstance().ExecuteNonQuery(parametros);
        }

        public static int getUltimoId
        {
            get
            {
                OleDbDataReader dr;
                dr = cnn.getDataReader("SELECT @@IDENTITY");
                if (dr.Read())
                {
                    return Convert.ToInt32(dr[0]);
                }
                else
                {
                    return 0;
                }
            }
        }

        public static string getMensaje
        {
            get
            {
                return "";
            }
        }

        public static void iniciarTransaccion()
        {
            cnn.getInstance().InitTransaction();
        }

        public static void denegarTrans()
        {
            cnn.getInstance().RollBackTransaction();
        }

        public static void confirmarTrans()
        {
            cnn.getInstance().CommitTransaction();
        }
    }
}
